//
//      ===  Demo LCDSmartie Plugin for c#  ===
//


using System;
using System.Diagnostics;
using System.Text;
using System.Runtime.InteropServices;

namespace processes
{
	/// <summary>
	/// Plugin for LCDSmartie to list active processes.
	/// </summary>
	public class LCDSmartie
	{
		private string lastWin;

		public LCDSmartie()
		{
			lastWin = String.Empty;
		}

		[ DllImport("user32.dll") ]
		private static extern IntPtr GetForegroundWindow();

		[ DllImport("user32.dll") ]
		private static extern int GetWindowText(int hWnd, StringBuilder text, int count);

		[ DllImport("user32.dll") ]
		private static extern UInt32 GetWindowThreadProcessId(
			Int32 hWnd,
			out Int32 lpdwProcessId
			);

		/// <summary>
		/// List all running processes by name.
		/// </summary>
		/// <param name="param1">Not used</param>
		/// <param name="param2">Not used</param>
		/// <returns></returns>
		public string function1(string param1, string param2)
		{
			string s = "";
			Process[] myProcesses = Process.GetProcesses();
			foreach(Process myProcess in myProcesses)
			{
				s += myProcess.ProcessName + ";";
			}
			return s;
		}

		/// <summary>
		/// Test if the process "process" is running 
		/// </summary>
		/// <param name="process">Wich process to check for</param>
		/// <param name="param2"></param>
		/// <returns>1 if process is running else 0.</returns>
		public string function2(string process, string param2)
		{
			Process[] myProcesses = Process.GetProcesses();
			foreach(Process myProcess in myProcesses)
			{
				if (myProcess.ProcessName.ToLower().Equals(process.ToLower()))
					return "1";
			}
			return "0";
		}
		
		/// <summary>
		/// Returns the title of the current window
		/// </summary>
		/// <param name="param1">not used</param>
		/// <param name="param2">not used</param>
		/// <returns>Title of current window</returns>
		public string function3(string param1, string param2)
		{
			try 
			{
				int handle = 0;
				StringBuilder Buff = new StringBuilder(256);
				handle = GetForegroundWindow().ToInt32();
				if ( GetWindowText(handle, Buff, 256) > 0 )
					return Buff.ToString();
			}
			catch (Exception) {;}
			return "";
		}
		
		/// <summary>
		/// Returns the process name of the current window
		/// </summary>
		/// <param name="param1">not used</param>
		/// <param name="param2">not used</param>
		/// <returns>Pocess name of current window</returns>
		public string function4(string param1, string param2)
		{
			try 
			{
				return (Process.GetProcessById(GetWindowProcessID(GetForegroundWindow().ToInt32()))).ProcessName;
			}
			catch (Exception) {;}
			return "";
		}

		/// <summary>
		/// Test if a specified process is the currently active process
		/// </summary>
		/// <param name="process">Which process</param>
		/// <param name="param2"></param>
		/// <returns>1 if process is focused else 0.</returns>
		public string function5(string process, string param2)
		{
			try 
			{
				if(process.ToLower().Equals((Process.GetProcessById(
					GetWindowProcessID(
					GetForegroundWindow().ToInt32()))).ProcessName.ToLower()))
					return "1";
		} 
			catch (Exception) {;}
			return "0";
		}

		/// <summary>
		/// Test if the current active process have changed.
		/// </summary>
		/// <param name="process">Which process</param>
		/// <param name="param2"></param>
		/// <returns>1 if process has changed else 0.</returns>
		public string function6(string process, string param2)
		{
			String curWin = function4("","");
			if(!lastWin.Equals(curWin))
			{
				lastWin=curWin;
				return "1";
			}
			return "0";
		}

		//
		// Define the minimum interval that a screen should get fresh data from our plugin.
		// The actual value used by Smartie will be the higher of this value and of the "dll check interval" setting
		// on the Misc tab.  [This function is optional, Smartie will assume 300ms if it is not provided.]
		// 
		public int GetMinRefreshInterval()
		{
			return 500; // 300 ms (around 3 times a second)
		}
	
		private static Int32 GetWindowProcessID(Int32 hwnd)
		{
			Int32 pid = 1;
			GetWindowThreadProcessId(hwnd, out pid);
			return pid;
		}
	}
}

